<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发送微博</title>
    <!-- import CSS (element ui的css)-->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">

</head>
<body>
<div>
    <h1>发微博页面</h1>
    <input type="text" placeholder="说点啥" v-model="weibo.content">
    <!--**********************************************-->
    <el-upload
            action="/upload"
            name="picFile"
            limit="9"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <!--**********************************************-->
    <input type="button" value="发微博" @click="insert()">
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>

<script>
    let v = new Vue({
        el: "body>div",
        data: {
            weibo: {content: "", urls: ""},
            fileList: [],
            /******************************************/
            dialogImageUrl: '',
            dialogVisible: false
            /******************************************/
        },
        methods: {
            handleSuccess(response, file, fileList) {
                //接收上传成功的图片
                v.fileList = fileList;
            },
            insert() {
                if (v.fileList.length == 0) {
                    alert('Please select');
                    return;
                }
                let arr = [];
                for (let file of v.fileList) {
                    arr.push(file.response);//file.response把每一个图片路径装进数组
                }
                //arr中插入多个图片路径
                v.weibo.urls = arr.toString();
                axios.post("/weibo/insert", v.weibo).then(
                    function (response) {
                        if (response.data==1){
                            alert("success!");
                            location.href = "/";
                        }else{
                         alert("请先登录!");
                         location.href = "/login.html";//跳转到登录页面
                        }
                    }
                )
            },
            /******************************************/
            handleRemove(file, fileList) {
                v.fileList=fileList;
                console.log(file, fileList);
                //发出删除图片的请求,file图片,file.response代表图片路径
                axios.post("/remove?url=" + file.response).then(function () {
                    console.log("删除完成!")
                })
            }
            ,
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            }
        }
    })
</script>
</html>